package validation.jexp;

import com.jgoodies.binding.PresentationModel;
import com.jgoodies.binding.adapter.BasicComponentFactory;
import com.jgoodies.binding.value.Trigger;
import com.jgoodies.binding.value.ValueModel;
import com.jgoodies.forms.builder.DefaultFormBuilder;
import com.jgoodies.forms.layout.FormLayout;

import javax.swing.*;
import java.awt.event.ActionEvent;

/**
 * JGoodies Binding: Buffering Presentation Model Example
 */
public class BufferingPresentationModelExample extends JPanel {

    private PersonBean personBean;
    private Trigger trigger;
    private PresentationModel presentationModel;

    public BufferingPresentationModelExample() {
        DefaultFormBuilder defaultFormBuilder = new DefaultFormBuilder(new FormLayout("p, 2dlu, p:g"));
        defaultFormBuilder.setDefaultDialogBorder();
        personBean = new PersonBean("Scott", "Delap");
        this.trigger = new Trigger();
        presentationModel = new PresentationModel(personBean, this.trigger);
        ValueModel firstNameAdapter = presentationModel.getBufferedModel("firstName");
        ValueModel lastNameAdapter = presentationModel.getBufferedModel("lastName");
        JTextField firstNameField = BasicComponentFactory.createTextField(firstNameAdapter);
        JTextField lastNameField = BasicComponentFactory.createTextField(lastNameAdapter);
        defaultFormBuilder.append("First Name: ", firstNameField);
        defaultFormBuilder.append("Last Name: ", lastNameField);
        defaultFormBuilder.append(new JButton(new FlushBufferAction()), 3);
        defaultFormBuilder.append(new JButton(new CommitBufferAction()), 3);
        defaultFormBuilder.append(new JButton(new ShowValueHolderValueAction()), 3);
        add(defaultFormBuilder.getPanel());
    }

    private class CommitBufferAction extends AbstractAction {
        private CommitBufferAction() {
            super("Commit Buffer");
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            trigger.triggerCommit();
        }
    }

    private class FlushBufferAction extends AbstractAction {
        private FlushBufferAction() {
            super("Flush Buffer");
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            trigger.triggerFlush();
        }
    }

    private class ShowValueHolderValueAction extends AbstractAction {
        private ShowValueHolderValueAction() {
            super("Show Value");
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            StringBuffer message = new StringBuffer();
            message.append("<html>");
            message.append("<b>First Name:</b>");
            message.append(personBean.getFirstName());
            message.append("<br><b>Last Name:</b>");
            message.append(personBean.getLastName());
            message.append("<br><b>Is Buffering:</b>");
            message.append(presentationModel.isBuffering());
            message.append("</html>");
            JOptionPane.showMessageDialog(null, message.toString());
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            JGoodiesValidationUtils.showFrame("Buffering Presentation Model Example", new BufferingPresentationModelExample());
        });
    }
}

